package com.study.controller;

import com.alibaba.fastjson.JSON;
import com.study.domain.Order;
import com.study.domain.Product;
import com.study.service.OrderService;
import com.study.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;


/**
 * @author MI
 * @version 1.0
 * @date 2020/10/18 17:30
 */
// @RestController
@Slf4j
public class OrderControler {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    ProductService productService;

    @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("接收到 {} 商品的下单请求", pid);
        //调用商品查询商品信息
        Product product = productService.findByPid(pid);
        log.info("查询到{}商品信息是", pid, JSON.toJSONString(product));
        final com.study.domain.Order order = new com.study.domain.Order();
        log.info("FeignClient 测试");
        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setNumber(1);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        orderService.createOrder(order);
        log.info("订单完成{}", JSON.toJSONString(order));
        return order;
    }
    /**
     * ribbon实现负载均衡
     *
     * @param pid
     * @return
     */
   /* @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("接收到 {} 商品的下单请求", pid);
        //调用商品查询商品信息
        final List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        final ServiceInstance instance = instances.get(0);
        // Product product = restTemplate.getForObject(instance.getUri() + "/product/" + pid, Product.class);
        Product product = restTemplate.getForObject("http://service-product/product/" + pid, Product.class);
        log.info("查询到{}商品信息是", pid, JSON.toJSONString(product));
        final com.study.domain.Order order = new com.study.domain.Order();

        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setNumber(1);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        orderService.createOrder(order);
        log.info("订单完成{}", JSON.toJSONString(order));
        return order;
    }*/
   /* @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info("接收到 {} 商品的下单请求", pid);
        //调用商品查询商品信息
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info("查询到{}商品信息是", pid, JSON.toJSONString(product));
        final com.study.domain.Order order = new com.study.domain.Order();

        order.setUid(1);
        order.setUsername("测试用户");

        order.setPid(pid);
        order.setNumber(1);
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        orderService.createOrder(order);
        log.info("订单完成{}", JSON.toJSONString(order));
        return order;
    }*/
}
